package com.zxy.ziems.common.pojo.enums;

import com.zxy.ziems.common.validation.EnumerableValue;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据类型
 * @author 10348359
 * @create 2024-04-12 15:45
 */
public enum DataEnum implements EnumerableValue<String> {
    /**
     * 数据类型
     */
    BOOL("bool", 1),
    INT8("byte", 1),
    UINT8("ubyte", 1),
    INT16("short", 2),
    UINT16("ushort", 2),
    INT32("int", 4),
    UINT32("uint", 4),
    INT64("long", 8),
    INT64_1("long_1", 8), //电能算法
    FLOAT32("float", 4),
    FLOAT64("double", 8),
    STRING("string", 1);

    private static final Map<String, DataEnum> VALUES;

    static {

        final DataEnum[] values = values();
        VALUES = new HashMap<>();
        for (DataEnum item : values) {
            final String value = item.code;
            if (VALUES.containsKey(value)) {
                throw new AssertionError("value already in use: " + value);
            }
            VALUES.put(value, item);
        }
    }

    /**
     * 名称
     */
    private final String code;

    /**
     * 长度
     */
    private final Integer length;

    DataEnum(String code, Integer length) {
        this.code = code;
        this.length = length;
    }

    public String getCode() {
        return this.code;
    }

    public Integer getLength() {
        return length;
    }

    @Override
    public String toString() {
        return this.code;
    }

    /**
     * 根据类型名获取枚举对象
     *
     * @param type 类型名
     * @return 枚举对象
     */
    public static DataEnum getBy(String type) {
        if (type.length() <= 0 || !VALUES.containsKey(type)) {
            throw new IllegalArgumentException("unknown data type: " + type);
        }
        return VALUES.get(type);
    }

    @Override
    public String getEnumerableValue() {
        return code;
    }
}
